using System;
using System.Collections;
using System.Collections.Generic;
using HerenityCode.Reference.GO;
using UnityEngine;
using UnityEngine.EventSystems;

namespace HerenityCode.Tool
{
    namespace GO
    {
        public class MoveObject : MonoBehaviour
        {
            [HideInInspector] public bool isDrag;
            private GameObject moveTarget;
            private Vector3 mousePos;
            private Vector3 oriScreenPos;
            Vector3 tempObjectPosition;
            private GameObjectMessage msg = new GameObjectMessage();


            void Update()
            {
                SelectObject();
                MoveAndRotateObject();
                if (Input.GetMouseButtonUp(1))
                {
                    isDrag = false;
                    moveTarget = null;
                }
            }

            // 选择物体
            public void SelectObject()
            {
                msg = GetGameObject.MouseClickObject("Drag");
                if (moveTarget != null)
                {
                    mousePos = msg.mousePos;
                    isDrag = true;
                    moveTarget = msg.TarGO;
                    oriScreenPos = msg.oriScreenPos;
                    tempObjectPosition = msg.tempObjectPosition;
                }
            }

            // 移动和旋转物体
            public void MoveAndRotateObject()
            {
                if (Input.GetMouseButton(0))
                {
                    #region 拖拽和延轴拖拽

                    if (isDrag && moveTarget && !EventSystem.current.IsPointerOverGameObject())
                    {
                        //if (Input.GetKey(KeyCode.X))
                        //{
                        //    moveTarget.transform.position = Camera.main.ScreenToWorldPoint(oriScreenPos);
                        //    Vector3 newPosX = new Vector3
                        //        (
                        //        oriScreenPos.x + Input.mousePosition.x - mousePos.x,
                        //        Camera.main.WorldToScreenPoint(moveTarget.transform.position).y,
                        //        Camera.main.WorldToScreenPoint(moveTarget.transform.position).z
                        //        );
                        //    Vector3 newWorldPos = Camera.main.ScreenToWorldPoint(newPosX);
                        //    tempObjectPosition = moveTarget.transform.position;
                        //    moveTarget.transform.position = new Vector3(Vector3.Project(newWorldPos, Vector3.right).x, tempObjectPosition.y, tempObjectPosition.z);

                        //}
                        //else if (Input.GetKey(KeyCode.Y))
                        //{
                        //    moveTarget.transform.position = Camera.main.ScreenToWorldPoint(oriScreenPos);
                        //    Vector3 newPosY = new Vector3
                        //        (
                        //        Camera.main.WorldToScreenPoint(moveTarget.transform.position).x,
                        //        oriScreenPos.y + Input.mousePosition.y - mousePos.y,
                        //        Camera.main.WorldToScreenPoint(moveTarget.transform.position).z
                        //        );
                        //    Vector3 newWorldPos = Camera.main.ScreenToWorldPoint(newPosY);
                        //    tempObjectPosition = moveTarget.transform.position;
                        //    moveTarget.transform.position = new Vector3(tempObjectPosition.x, Vector3.Project(newWorldPos, Vector3.up).y, tempObjectPosition.z);
                        //}
                        //else if (Input.GetKey(KeyCode.Z))
                        //{
                        //    float z = tempObjectPosition.z;
                        //    z += (Input.mousePosition - mousePos).x * 0.001f;
                        //    moveTarget.transform.position = new Vector3(tempObjectPosition.x, tempObjectPosition.y, Vector3.ProjectOnPlane(new Vector3(0, 0, z), Vector3.up).z);
                        //}
                        //else
                        //{
                        Vector3 newPos = oriScreenPos + Input.mousePosition - mousePos;
                        Vector3 newWorldPos = Camera.main.ScreenToWorldPoint(newPos);
                        moveTarget.transform.position = newWorldPos;
                        //}
                    }

                    #endregion
                }
            }
        }
    }
}